<?php

namespace app\common\logic\meet;

use addons\litestore\model\Litestoreorder;

class Cron
{
    protected $error = '';

    public function addCron($name, $event, $execute_time, $relate_id)
    {
        $data = [
            'name' => $name,
            'event' => $event,
            'execute_time' => $execute_time,
            'relate_id' => $relate_id
        ];

        (new \app\common\model\meet\Cron())->save($data);
    }

    /**
     * 商城-订单取消
     * @param $order_id
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function orderCancel($order_id)
    {
        if (empty($order_id)) {
            return false;
        }

        $model = new Litestoreorder();

        $order = $model->where('id', $order_id)->find();

        if (empty($order)) {
            return false;
        }

        $order->cancel($order['user_id'], $order_id);

        return true;
    }

    /**
     * 商城-订单完成
     * @param $order_id
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function orderComplete($order_id)
    {
        if (empty($order_id)) {
            return false;
        }

        $model = new Litestoreorder();

        $order = $model->where('id', $order_id)->find();

        if (empty($order)) {
            return false;
        }

        $order->finish($order['user_id'], $order_id);

        return true;
    }

    /**
     * 商城-下单成功发送消息
     * @param $order_id
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function orderSendMessage($order_id)
    {
        if (empty($order_id)) {
            return false;
        }

        $logic = new Message();

        $logic->mallOrderPay($order_id);

        return true;
    }

    /**
     * 预约-取消订单
     * @param $order_id
     * @return bool
     */
    public function meetOrderCancel($order_id)
    {
        if (empty($order_id)) {
            return false;
        }

        $logic = new Order();

        $logic->cancelOrder($order_id);

        return true;
    }

    /**
     * 预约-订单完成
     * @param $order_id
     * @return bool
     */
    public function meetOrderComplete($order_id)
    {
        if (empty($order_id)) {
            return false;
        }

        $logic = new Order();

        $logic->completeOrder($order_id);

        return true;
    }

    /**
     * 预约-下单成功发送消息
     * @param $order_id
     * @return bool
     */
    public function meetOrderSendMessage($order_id)
    {
        if (empty($order_id)) {
            return false;
        }

        $logic = new Message();

        $logic->meetOrderPay($order_id);

        return true;
    }

    public function getError()
    {
        return $this->error;
    }
}